package cn.gson.oasys.services.daymanage.impl;

import cn.gson.oasys.model.dao.daymanagedao.DaymanageDao;
import cn.gson.oasys.model.dao.user.UserDao;
import cn.gson.oasys.model.entity.schedule.ScheduleList;
import cn.gson.oasys.model.entity.user.User;
import cn.gson.oasys.services.daymanage.DaymanageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DaymanageServiceImpl implements DaymanageService {

    @Autowired
    UserDao userDao;

    @Autowired
    DaymanageDao daymanageDao;

    //记日志
    Logger log = LoggerFactory.getLogger(getClass());

    //获取用户的计划表
    public List<ScheduleList> aboutmeschedule(Long userId) {

        User user = userDao.findOne(userId);

        List<User> users = new ArrayList<>();

        users.add(user);

        List<ScheduleList> aboutmerc = new ArrayList<>();

        //获取自己的计划表和其他人的计划表
        List<ScheduleList> myschedule = daymanageDao.findByUser(user);
        List<ScheduleList> otherschedule = daymanageDao.findByUsers(users);

        for (ScheduleList scheduleList : myschedule) {
            aboutmerc.add(scheduleList);
        }

        for (ScheduleList scheduleList : otherschedule) {
            aboutmerc.add(scheduleList);
        }

        for (ScheduleList scheduleList : aboutmerc) {
            User user1 = scheduleList.getUser();
            scheduleList.setUsername(user1.getRealName());
        }
        return aboutmerc;
    }

    //计算讨论区个数
    @Override
    public void save(ScheduleList scheduleList) {
        daymanageDao.save(scheduleList);
    }
}
